#pragma once

#include <memory>
#include <httplib.h>


#include "brpc.h"
#include "etcd_path.h"
#include "home.pb.h"


class HomeModule{
private:
    std::shared_ptr<brpc::Channel> _channel;
    std::shared_ptr<home::HomeService_Stub> _homeservice_stub;
public:
    using ptr = std::shared_ptr<HomeModule>;
    HomeModule(const std::string& etcd_host){
        _channel = lyt::build_etcd(etcd_host, lyt::home_path);
        _homeservice_stub = make_shared<home::HomeService_Stub>(_channel.get());
    }
    void UserBehaviorAnalysis(const httplib::Request& req,httplib::Response& res){
        // 用户行为分析模块




        
    }
    void homeSearch(const httplib::Request& req,httplib::Response& res){
        /*
            1. 创建proto请求对象与响应对象
            2. 解析二进制数据
            3. 交给验证码模块
        */
        home::SearchRequest searchrequest;
        home::SearchResponse searchresponse;
        if (!searchrequest.ParseFromArray(req.body.data(), req.body.size())) {
            res.status = 400;
            res.set_content("Invalid data", "text/plain");
            return;
        }
        brpc::Controller cntl;
        _homeservice_stub->Search(&cntl, &searchrequest, &searchresponse, nullptr);
        if (cntl.Failed()) {
            res.status = 500;
            res.set_content("RPC Failed: " + cntl.ErrorText(), "text/plain");
            return;
        }
        string res_bin;
        searchresponse.SerializeToString(&res_bin);
        res.status = 200;
        res.set_content(res_bin, "application/octet-stream");
    }

};